<?

//Data loading
$title='KAS - BON';
$title2='BON SEMENTARA';
//if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
//else $perush = 'PT. MASAJI PRAYASA CARGO';

$perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport("P","mm","Letter");
$pdf->AddPage();
$Result = GridList ();
$pdf->GridTable($Result);
$pdf->Output();


function GridList () {
            $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
                    //$SQL = "SELECT * FROM ais_advance WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";

                    $SQL = "SELECT * 
                            FROM ais_advance a 
                            JOIN tblsurveyassgn_activity b ON a.survey_act_id = b.survey_act_id 
                            WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";
                            

                    return $DBConnection->dbc->get_results($SQL,ARRAY_A);
}

function ReadHeader($nobkt) {
  global $DBConnection;
  $SQL = "SELECT *,DATE_FORMAT(tglbkt,'%d %M, %Y') as spell_date  FROM ais_advance WHERE nobkt='$nobkt' LIMIT 1";
  return $DBConnection->dbc->get_row($SQL);
}


class PDFReport extends PDF {

    function Header() {
        global $title,$title2,$perush,$page,$DEFINE_FONT, $DBConnection;
        if ($DEFINE_FONT==0) {
            $this->AddFont('Arial','','arialn.php');
            $DEFINE_FONT=1;
        }
        $Field = ReadHeader(GetParam("nobkt",""));
                $this->spell_date = strtoupper($Field->spell_date);
                list($this->year,$this->month,$this->day) = explode("-",$Field->tglbkt);

                //set left margin
		        $this->SetLeftMargin(15);
				
                $this->SetFont('Arial','',10);
                $this->Cell(50,1,"",0,0,'L',0);
                $this->Ln();
                $this->Cell(50,1,"",0,0,'L',0);
                $this->Ln();
                $this->Cell(50,5,$perush,0,0,'C',0);
                $this->Cell(130,5,GetParam("nobkt",""),0,0,'R',0);
                $this->Ln();
                $this->Cell(50,5,"JAKARTA",0,0,'C',0);
                $this->Ln();
                $this->SetFont('Arial','',16);
                //$this->Cell(200,7,$title,0,1,'C',0);
                $this->Cell(180,7,$title2,0,1,'C',0);
                $this->SetFont('Arial','',10);
                $this->SetLeftMargin(5);
                $this->Ln();
                $this->SetLeftMargin(5);
                //$this->Line(5,30,210,30);
                //$this->Ln();

        //Header
        //Column widths
            /*$SQL = "SELECT DISTINCT curr as job_costing_curr FROM ais_advance a
                    JOIN tbljoborder_costing b ON a.jo_costing_id = b.jo_costing_id
                    WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";*/

            $SQLCurr = "SELECT DISTINCT curr as job_costing_curr FROM ais_advance a
                        WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";

            $Curr = $DBConnection->dbc->get_var($SQLCurr);


            $w=array(10,130,40);
			
			//set left margin
		    $this->SetLeftMargin(15);			
			
            $this->SetFont('Arial','',8);
            $this->Cell(130,7,"NO BUKTI: ".GetParam("nobkt",""),"LBT",0,'L',0);
            $this->Cell(50,7,"TANGGAL: ".$this->day."/".$this->month."/".$this->year,"RBT",0,'R',0);
            $this->Ln();
            $this->Cell($w[0],7,"NO.",1,0,'C',0);
            //$this->Cell($w[1],7,"URAIAN",1,0,'C',0);
            $this->Cell($w[1],7,"ACTIVITIES NAME",1,0,'C',0);
            $this->Cell($w[2],7,"JUMLAH ($Curr)",1,0,'C',0);
            $this->Ln();
    }

    function GridTable($MyResult) {
        //Column widths
        global $DBConnection,$DBGeneral;
        $w=array(10,130,40);
        //Data
		
		$urut = 1;
		
		//set left margin
		$this->SetLeftMargin(15);
		
        if ($MyResult)
		{
			foreach($MyResult as $row)
			{
			
				$Total = $Total + $row["jumlah"];
				$this->Cell($w[0],7,$urut.".",'LR',0,'R');
				$this->Cell($w[1],7,$row["uraian"],'LR',0,'L');            
				$this->Cell($w[2],7,FormatNumber($row["jumlah"]),'LR',0,'R');
				
				$this->SetFont('Arial','',7);
				
				/*
				$this->Ln(3);
				$this->Cell($w[0],7,"",'LR',0,'R');
				$this->Cell($w[1],7,"JO Code= ".$row["jo_code"],'LR',0,'L');
				$this->Cell($w[2],7,"",'LR',0,'R');
				
				$this->Ln(3);
				$this->Cell($w[0],7,"",'LR',0,'R');
				$this->Cell($w[1],7,"Project= ".$row["project_name"],'LR',0,'L');
				$this->Cell($w[2],7,"",'LR',0,'R');
				
				$this->Ln(3);
				$this->Cell($w[0],7,"",'LR',0,'R');
				$this->Cell($w[1],7,"Customer= ".$row["CUNAME"],'LR',0,'L');
				$this->Cell($w[2],7,"",'LR',0,'R');
				
				$this->Ln(3);
				$this->Cell($w[0],7,"",'LR',0,'R');
				$this->Cell($w[1],7,"Vessel/Voyage= ".$row["vessel"],'LR',0,'L');
				$this->Cell($w[2],7,"",'LR',0,'R');
				*/
				
				$this->Ln(5);
				
				//$this->Cell($w[1],7,"( JO Code= ".$row["jo_code"]."; Project= ".$row["project_name"]."; Customer= ".$row['CUNAME']."; Vessel/Voyage: ".$row['vessel']."; Currency: ".$row['job_costing_curr']." )",'LR',0,'L');
				//$this->Cell($w[1],7,"( JO Code= ".$row["jo_code"]."; Project= ".$row["project_name"]."; Customer= ".$row['CUNAME']."; Vessel/Voyage: ".$row['vessel'].")",'LR',0,'L');
				//$this->Cell($w[2],7,"",'LR',0,'R');
				$this->SetFont('Arial','',8);

				$urut++;
				
			}
		
	    }
            $this->Cell($w[0],7,"",'LR',0,'R');
            $this->Cell($w[1],7,"",'LR',0,'L');
        $this->Cell($w[2],7,"",'LR',0,'R');
            $this->Ln();
                $cc = new intToRupiah;
                $GetCurrency = $DBConnection->dbc->get_var("SELECT DISTINCT tunam FROM tblcurrency, ais_advance WHERE ais_advance.nobkt='".GetParam("nobkt","")."' AND tblcurrency.tusymbol=ais_advance.curr AND ktrasl='".GetOfficeID()."'");
                //$DBConnection->dbc->debug();
                $cc->setTail("");
                $cc->setNumber($Total);
                $SpellNumber = ucfirst(strtoupper(trim($cc->currency)));

            $this->Cell($w[0],7,"",'LR',0,'R');
            $this->Cell($w[1],7,"TERBILANG:     ".$SpellNumber,'LR',0,'L');
        $this->Cell($w[2],7,"",'LR',0,'R');
            $this->Ln();
            $this->Cell($w[0]+$w[1],7,"TOTAL:",1,0,'R',0);
            $this->Cell($w[2],7,FormatNumber($Total,2),1,0,'R',0);
            $this->Ln();
            $this->Ln();

                /*
                $GetPerson = $DBConnection->dbc->get_row("SELECT * FROM tblprofile", ARRAY_A);
                
				$totalAll_menyetujui = $Total;
				$totalAll_tgjwb = $Total;
				  
                if($totalAll_menyetujui <= 1000000)
				{
					$ttd_menyetujui = $GetPerson["ttd_setuju_survey_kur"];
				}
				
				else 
				{
				    $ttd_menyetujui = $GetPerson["ttd_setuju_survey_lbh"];
				}
				
				if($totalAll_tgjwb <= 15000000)
				{
					$ttd_tgjwb = $GetPerson["ttd_tgjwb_survey_kur"];
				}
				
				else 
				{
				    $ttd_tgjwb = $GetPerson["ttd_tgjwb_survey_lbh"];
				}
				*/
				
				
				//CURRENCY
				$SQLCurr = "SELECT DISTINCT curr as job_costing_curr FROM ais_advance a
							WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";

				$Curr = $DBConnection->dbc->get_var($SQLCurr);
				
				//Currency Rate
				$SQLGetKurs = "SELECT * FROM ais_kurssi WHERE curr = 'USD' ORDER BY tglbkt DESC";
				$ResultGetKurs  = $DBConnection->dbc->get_row($SQLGetKurs,ARRAY_A);   

				$kursUSD = $ResultGetKurs['rate'];
				
				
				if($Curr == "USD")
				{
					$totalAll_menyetujui = $Total * $kursUSD;
				    $totalAll_tgjwb = $Total * $kursUSD;
				}
				
				else
				{
					$totalAll_menyetujui = $Total;
				    $totalAll_tgjwb = $Total;
				}
				
				
				
				$ttd_menyetujui = "";
				$ttd_tgjwb = "";
				
				
				
				
				$SQLGetSetujuBayar = "SELECT * FROM tblreportsign as a 
				                      LEFT JOIN ".$DBGeneral.".tblstaff as b on a.staff_id = b.staff_id   
				WHERE a.assignment = 'setuju_bayar' AND a.report_kind = 'bs_survey' ORDER BY a.report_sign_id ASC ";
				
				$GetPersonSetujuBayar = $DBConnection->dbc->get_results($SQLGetSetujuBayar, ARRAY_A);
                
				
				
				if(!empty($GetPersonSetujuBayar))
				{
				
					foreach($GetPersonSetujuBayar as $idx => $val)
					{
					   $range_left = $val['range_left'];
					   $range_right = $val['range_right'];
					   
					   if(($totalAll_menyetujui >= $range_left) && ($totalAll_menyetujui <= $range_right))
					   {
							$ttd_menyetujui = $val["nama"];
					   }	
					
					}
				
			    }
				
				
				$SQLGetTanggungJawab = "SELECT * FROM tblreportsign as a 
				                      LEFT JOIN ".$DBGeneral.".tblstaff as b on a.staff_id = b.staff_id  
				WHERE a.assignment = 'penanggung_jawab' AND a.report_kind = 'bs_jo' ORDER BY a.report_sign_id ASC ";
				
				$GetPersonTanggungJawab = $DBConnection->dbc->get_results($SQLGetTanggungJawab, ARRAY_A);
                
                
				if(!empty($GetPersonTanggungJawab))
				{
				
					foreach($GetPersonTanggungJawab as $idx => $val)
					{
					   $range_left = $val['range_left'];
					   $range_right = $val['range_right'];
					   
					   if(($totalAll_tgjwb >= $range_left) && ($totalAll_tgjwb <= $range_right))
					   {
							$ttd_tgjwb = $val["nama"];
					   }	
					
					}
				
			    }
				

                $this->Cell(60,5,"Setuju Bayar",1,0,'C',0);
                $this->Cell(60,5,"Penanggung Jawab",1,0,'C',0);
                $this->Cell(60,5,"Penerima",1,0,'C',0);
                $this->Ln();
                $this->Cell(10,5,"Tanggal",1,0,'C',0);
                $this->Cell(50,5,"",1,0,'C',0);
                $this->Cell(10,5,"Tanggal",1,0,'C',0);
                $this->Cell(50,5,"",1,0,'C',0);
                $this->Cell(10,5,"Tanggal",1,0,'C',0);
                $this->Cell(50,5,"",1,0,'C',0);
                $this->Ln();
                $this->Cell(10,5,"Nama",1,0,'C',0);
                $this->Cell(50,5,$ttd_menyetujui,1,0,'C',0);
                $this->Cell(10,5,"Nama",1,0,'C',0);
                $this->Cell(50,5,$ttd_tgjwb,1,0,'C',0);
                $this->Cell(10,5,"Nama",1,0,'C',0);
                $this->Cell(50,5,$row["debnam"],1,0,'C',0);
                $this->Ln();
                $this->Cell(10,8,"Ttd",1,0,'C',0);
                $this->Cell(50,8,"",1,0,'C',0);
                $this->Cell(10,8,"Ttd",1,0,'C',0);
                $this->Cell(50,8,"",1,0,'C',0);
                $this->Cell(10,8,"Ttd",1,0,'C',0);
                $this->Cell(50,8,"",1,0,'C',0);


                /*$this->Cell(65,7,"DISETUJUI",0,0,'C',0);
                $this->Cell(65,7,"MENGETAHUI",0,0,'C',0);
                $this->Cell(65,7,"PENERIMA",0,0,'C',0);
            $this->Ln();$this->Ln();$this->Ln();
                $this->Cell(65,7,"( ".$GetPerson["ttdbs1"]." )",0,0,'C',0);
                $this->Cell(65,7,"( ".$GetPerson["ttdbs2"]." )",0,0,'C',0);
                $this->Cell(65,7,"( ".$row["debnam"]." )",0,0,'C',0);
                */


        //Closure line
    }

    function Footer() {
        //Closure line
        //$this->Cell(205,0,'','T');
    }

}


?>
